package com.itic.hpp.user;

import com.itic.appbase.framework.Base.BaseCode;
import com.itic.appbase.framework.type.*;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mobile.base.BaseMobile;
import com.itic.system.collage.collage.persistence.model.Collage;
import com.itic.system.collage.collage.service.CollageService;
import com.itic.system.order.order.persistence.model.Order;
import com.itic.system.order.order.service.OrderService;
import com.itic.system.user.balanceLog.persistence.model.UserBalanceLog;
import com.itic.system.user.balanceLog.persistence.model.UserBalanceLogExample;
import com.itic.system.user.balanceLog.service.UserBalanceLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/hpp/user/balance")
public class UserBalanceInterface extends BaseMobile{

    @Autowired
    private UserBalanceLogService userBalanceLogService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private CollageService collageService;

    /**
     * 用户自卖收入明细
     * @param userId
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/sellIncome")
    @ResponseBody
    public Map<String, Object> sellIncome(String userId, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            // 效验参数
            if (StringHelper.isNoneBlank(userId)) {
                UserBalanceLogExample example = new UserBalanceLogExample();
                this.handlePageQueryDefault(request,example);
                List<UserBalanceLog> userBalanceLogs = this.userBalanceLogService.findUserBalanceByUserId(example,userId, FlowType.InCome, FlowMethod.OrderTrade, FlowSource.Order);
                Map<String,Object> data = new HashMap<>();
                List<Map<String,Object>> list = new ArrayList<>();
                for(UserBalanceLog userBalanceLog : userBalanceLogs){
                    Map<String,Object> map = new HashMap<>();
                    Order order = this.orderService.getOrderByOrderNo(userBalanceLog.getOrderNo());
                    Collage collage = this.collageService.get(order.getCollageId());
                    map.put("title",collage.getTitle());
                    map.put("money",userBalanceLog.getMoney());
                    map.put("time",userBalanceLog.getCreateTime());
                    list.add(map);
                }
                double totalMoney = this.userBalanceLogService.getUserTotalIncomeMoney(userId);
                data.put("totalMoney",totalMoney);
                data.put("incomeList",list);
                resultMap.put(BaseCode.DATA, data);
                resultMap.put(BaseCode.CODE, BaseCode.SUCCESS_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.SUCCESS_ERROR_MSG);
            } else {
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        } catch (Exception e) {
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("UserBalanceInterface", "sellIncome", e.toString(), "自卖收入", e);
        }
        return resultMap;
    }


}
